<template>
  <div>
    <div class="top">添加供货厂商</div>
    <div style="margin: 20px"></div>
    <el-form
      :label-position="labelPosition"
      label-width="100px"
      :model="formLabelAlign"
      class="form"
      :rules="rules"
    >
      <el-form-item label="联系人:" prop="ve_contact">
        <el-input v-model="formLabelAlign.ve_contact"></el-input>
      </el-form-item>
      <el-form-item label="厂商名称:" prop="ve_name">
        <el-input v-model="formLabelAlign.ve_name"></el-input>
      </el-form-item>
      <el-form-item label="联系电话:" prop="ve_phone">
        <el-input v-model="formLabelAlign.ve_phone"></el-input>
      </el-form-item>
      <el-form-item label="固话:" prop="ve_Landline_telephones">
        <el-input v-model="formLabelAlign.ve_Landline_telephones"></el-input>
      </el-form-item>
      <el-form-item label="邮箱:" prop="ve_Email">
        <el-input v-model="formLabelAlign.ve_Email"></el-input>
      </el-form-item>
      <el-form-item label="厂商地址:" prop="ve_address">
        <el-input v-model="formLabelAlign.ve_address"></el-input>
      </el-form-item>
      <el-form-item label="备注:">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="formLabelAlign.ve_beizhu"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('numberValidateForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  AddSupplyApi,
  updatedSupplyApi,
} from "@/http/merchant/supply";
export default {
  data() {
    // let supplierName = (rule, value, callback) => {
    //   if (/[\u4e00-\u9fa5]+$/.test(value)) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的名字"));
    // };
    // let Contact = (rule, value, callback) => {
    //   if (/[\u4e00-\u9fa5]+$/.test(value)) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的名字"));
    // };
    // let Contactnumber = (rule, value, callback) => {
    //   if (
    //     /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(
    //       value
    //     )
    //   ) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的电话号码"));
    // };
    // let Fixed = (rule, value, callback) => {
    //   if (
    //     /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(
    //       value
    //     )
    //   ) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的电话号码"));
    // };
    // let Mailbox = (rule, value, callback) => {
    //   if (
    //     /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(
    //       value
    //     )
    //   ) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的邮箱"));
    // };
    // let manufacturerAddress = (rule, value, callback) => {
    //   if (/[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(value)) {
    //     return callback();
    //   }
    //   callback(new Error("请输入正确的地址"));
    // };
    return {
      labelPosition: "right",
      formLabelAlign: {
        // ve_Email: "",
        // ve_Landline_telephones: "",
        // ve_address: "",
        // ve_beizhu: "",
        // ve_contact: "",
        // ve_name: "",
        // ve_phone:""
      },
      textarea: "",
      rules: {
        ve_name: [
          { required: true, message: "请输入正确的名字", trigger: "blur" },
          // { validator: supplierName, trigger: "blur" },
        ],
       ve_contact: [
          { required: true, message: "请输入正确的名字", trigger: "blur" },
          // { validator: Contact, trigger: "blur" },
        ],
        ve_phone: [
          { required: true, message: "请输入正确的电话号码", trigger: "blur" },
          // { validator: Contactnumber, trigger: "blur" },
        ],
        ve_Landline_telephones: [
          { required: true, message: "请输入正确的电话号码", trigger: "blur" },
          // { validator: Fixed, trigger: "blur" },
        ],
        ve_Email: [
          { required: true, message: "请输入正确的邮箱", trigger: "blur" },
          // { validator: Mailbox, trigger: "blur" },
        ],
        ve_address: [
          { required: true, message: "请输入正确的地址", trigger: "blur" },
          // { validator: manufacturerAddress, trigger: "blur" },
        ],
      },
    };
  },
  activated() {
    this.init()
  },
  methods: {
    // 判断是编辑还是添加
    init() {
      if(this.$route.query) {
       console.log(this.$route.query,'9999')
        this.formLabelAlign = this.$route.query
      }
    },

    //提交
    async submitForm() {
      if(this.$route.query.ve_id) {
        // 编辑
        updatedSupplyApi(this.formLabelAlign).then((res) => {
           this.$router.push({ path: "/system-2/system-2-4" })
           console.log(res)
        })
      } else {
        // 添加
        let data = await AddSupplyApi(this.formLabelAlign.ve_Email,this.formLabelAlign.ve_Landline_telephones,
        this.formLabelAlign.ve_address,this.formLabelAlign.ve_beizhu,this.formLabelAlign.ve_contact
        ,this.formLabelAlign.ve_name,this.formLabelAlign.ve_phone)
                        .then((res) => {
                          console.log(res)
                          this.$router.push({ path: "/system-2/system-2-4" })
                        })
        if (data.data.code != 200) throw new Error("添加失败");
        this.$message.success("添加成功")
      }
      
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  padding: 0 20px;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #eee;
  border: 1px solid #ddd;
  border-bottom: none;
  line-height: 50px;
}

.form {
  margin: auto;
  width: 400px;
}
</style>